package com.baby.movie.app.ui;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.VideoView;

import com.baby.movie.R;
import com.baby.movie.model.Video;
import com.baby.movie.util.Constants;
import com.baby.movie.widget.MMediaController;
import com.baby.movie.widget.MMediaController.OnShowListener;
import com.common.util.AndroidUtil;

public class MTDetailActivity extends BaseActivity {

	private VideoView mVideoView;

	private TextView mTvTitle, mTvAddTime, mTvCategory, mTvSummary;

	private View mVVideo;

	private Video mVideo;

	private LayoutParams mHalfParams, mFullScreenParams;

	private MMediaController mMediaController;
	
	private boolean mIsReady;

	@Override
	protected boolean showHome() {
		return true;
	}

	@Override
	protected int getContentViewRes() {
		return R.layout.activity_mt_detail;
	}

	@Override
	protected void findViews() {
		mVideoView = (VideoView) findViewById(R.id.videoView);
		mMediaController = (MMediaController) findViewById(R.id.mediacontroller);
		mMediaController.setVisibility(View.GONE);
		mVVideo = findViewById(R.id.vVideo);
		mTvTitle = (TextView) findViewById(R.id.tvTitle);
		mTvAddTime = (TextView) findViewById(R.id.tvAddTime);
		mTvCategory = (TextView) findViewById(R.id.tvType);
		mTvSummary = (TextView) findViewById(R.id.tvSummary);
	}

	@Override
	protected void initialize() {
		mVideo = (Video) getIntent().getSerializableExtra(Constants.MT);
		setTitle(mVideo.Title);

		String mp4 = mVideo.Mp4Url;
		if (!AndroidUtil.isEmpty(mp4)) {
			mMediaController.setFullScreenClickListener(mFullScreenListener);
			// mMediaController.show(7000);

			mVideoView.setVideoPath(mp4);
			mMediaController.setMediaPlayer(mVideoView);
			mVideoView.setOnTouchListener(mOnViewdeoTouchListener);
			
			mVideoView.setOnPreparedListener(new OnPreparedListener() {
				
				@Override
				public void onPrepared(MediaPlayer mp) {
					mIsReady = true;
					mMediaController.show();
				}
			});
			
			mMediaController.setOnShowListenr(new OnShowListener() {
				
				@Override
				public void onShow() {
					mActionBar.show();
				}
				
				@Override
				public void onHide() {
					mActionBar.hide();
				}
			});
		}

		mTvTitle.setText(mVideo.Title);
		mTvAddTime.setText("上传时间：" + mVideo.AddTime);
		mTvCategory.setText("点击次数：" + mVideo.ClickCount);
		mTvSummary.setText(mVideo.Summary);
		
		mFullScreenParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	}

	private OnTouchListener mOnViewdeoTouchListener = new OnTouchListener() {

		private static final float DIS = 5;
		private float oldX, newX;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (!mIsReady) {
				return false;
			}
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				oldX = event.getX();
				return true;

			case MotionEvent.ACTION_UP:
				newX = event.getX();
				if (Math.abs(oldX - newX) < DIS) {
					if (mMediaController.isShowing()) {
						mMediaController.hide();
					} else {
						mMediaController.show();
					}
					return true;
				}
				oldX = 0;
				newX = 0;
				break;

			default:
				break;
			}
			return false;
		}
	};

	private OnClickListener mFullScreenListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (null == mHalfParams) {
				mHalfParams = (LayoutParams) mVVideo.getLayoutParams();
			}

			if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			} else {
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			}
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			mVVideo.setLayoutParams(mFullScreenParams);

		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			mVVideo.setLayoutParams(mHalfParams);
		}
		super.onConfigurationChanged(newConfig);
	}

}
